﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Scripting.Ast;
using Microsoft.Scripting;
using Microsoft.Linq.Expressions;

namespace DlrBasic.Parser.Ast
{
    class InteractiveLine : ProgramBase 
    {
        Statement[] statements;

        public InteractiveLine(SourceSpan span, Statement[] statements)
            : base(span)
        {
            this.statements = statements;
        }


        internal override Expression Generate(DlrBasicGenerator g)
        {
            if (statements.Length > 0)
            {
                g.GenerateStatementNumbers(this);

                return base.Generate(g);
            }
            else
                return Expression.Empty();
        }

        internal override void Visit(StatementsVisitor visitor)
        {
            visitor.InteractiveLine(this);
            for (int i = 0; i < statements.Length; i++)
            {
                statements[i].Visit(visitor);
            }
        }

        protected override Expression CreateDebugInfo(DlrBasicGenerator g)
        {
            int[] lineNumbers = new int[] { 0 };
            int[] states = new int[] { 0 };

            StatementNames namesVisitor = new StatementNames();
            Visit(namesVisitor);

            Expression debugInfo = Expression.New(typeof(Runtime.DlrBasicDebugInfo).GetConstructors()[0],
                    Expression.Constant(lineNumbers), Expression.Constant(states),
                    Expression.Constant(namesVisitor.Names.ToArray()));
            return debugInfo;
        }
    }
}
